package org.springframework.samples.petclinic.vets.model;

import com.azure.core.credential.AzureKeyCredential;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.DirectConnectionConfig;
import com.azure.cosmos.GatewayConnectionConfig;
import com.azure.spring.data.cosmos.config.AbstractCosmosConfiguration;
import com.azure.spring.data.cosmos.config.CosmosConfig;
import com.azure.spring.data.cosmos.core.ResponseDiagnostics;
import com.azure.spring.data.cosmos.core.ResponseDiagnosticsProcessor;
import com.azure.spring.data.cosmos.repository.config.EnableCosmosRepositories;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.lang.Nullable;

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

@Configuration
@EnableConfigurationProperties(CosmosProperties.class)
@EnableCosmosRepositories
@PropertySource("classpath:application.properties")
public class VetsAppConfiguration extends AbstractCosmosConfiguration {

  private static final Logger logger = LoggerFactory.getLogger(VetsAppConfiguration.class);

    @Autowired
    private CosmosProperties properties;

  private AzureKeyCredential azureKeyCredential;

  /**
   *
   * Bean definetion for CosmosClientBuilder
   * @return
   */
  @Bean
  public CosmosClientBuilder cosmosClientBuilder() {
    this.azureKeyCredential = new AzureKeyCredential(properties.getKey());
    DirectConnectionConfig directConnectionConfig = new DirectConnectionConfig();
    GatewayConnectionConfig gatewayConnectionConfig = new GatewayConnectionConfig();
    return new CosmosClientBuilder()
        .endpoint(properties.getUri())
        .credential(azureKeyCredential)
        .directMode(directConnectionConfig, gatewayConnectionConfig);
  }

  @Override
  public CosmosConfig cosmosConfig() {
    return CosmosConfig.builder()
        .enableQueryMetrics(properties.isPopulateQueryMetrics())
        .responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
        .build();
  }

  public void switchToSecondaryKey() {
    this.azureKeyCredential.update(properties.getSecondaryKey());
  }

  @Override
  protected String getDatabaseName() {
    return properties.getDatabase();
  }

    private static class ResponseDiagnosticsProcessorImplementation implements ResponseDiagnosticsProcessor {

    @Override
    public void processResponseDiagnostics(@Nullable ResponseDiagnostics responseDiagnostics) {
      logger.info("Response Diagnostics {}", responseDiagnostics);
    }
  }
}
